// pages/posts/post-detail/post-detail.js
// 引入数据模块
var postsData = require("../../../data/posts-data.js");
// 获取全局变量
var app = getApp();
var g_isPlayingMusic = app.globalData.g_isPlayingMusic;
var currentPostId = app.globalData.g_currentMusicPostId;
Page({

  /**
   * 页面的初始数据
   */
  data: {
    isPlayingMusic: false
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    var postId = options.id;
    var postData = postsData.postList[postId];
    this.setData(postData);
    // 收藏功能
    var postsCollected = wx.getStorageSync("posts_collected");
    // console.log(postsCollected);
    // 有文章阅读过
    if (postsCollected) {
      var postCollected = postsCollected[postId];
      // 点击的该文章阅读过
      if (postCollected) {
        this.setData({
          collected: postCollected
        })
        // 点击的该文章没有阅读过
      } else {
        postsCollected[postId] = false;
        wx.setStorageSync("posts_collected", postsCollected);
      }
      // 一篇文章都没阅读过
    } else {
      postsCollected = {};
      postsCollected[postId] = false;
      wx.setStorageSync("posts_collected", postsCollected);
    }
    this.setMusicMonitor();
    if (g_isPlayingMusic && currentPostId === postId) {
      this.setData({
        isPlayingMusic: true
      })
    }
  },
  setMusicMonitor: function() {
    //监听音乐播放事件
    var that = this;
    wx.onBackgroundAudioPlay(function() {
      that.setData({
        isPlayingMusic: true
      })
      g_isPlayingMusic = true;
      currentPostId = that.options.id;
    });

    wx.onBackgroundAudioPause(function() {
      that.setData({
        isPlayingMusic: false
      })
      g_isPlayingMusic = false;
      currentPostId = null;
    });
  },
  onCollectionTap: function(event) {
    this.getPostsCollectedSync();
    // this.getPostsCollectedAsy();
  },
  // 同步异步的区别，拿点击收藏做例子
  // 同步
  getPostsCollectedSync: function() {
    var postId = this.data.postId;
    var postsCollected = wx.getStorageSync("posts_collected");
    var postCollected = postsCollected[postId];
    // 点击取反操作
    postCollected = !postCollected;
    // 更新该篇文章的状态值
    postsCollected[postId] = postCollected;
    // this.showToast(postsCollected, postCollected)
    this.showToast(postsCollected, postCollected)
  },
  // 异步
  getPostsCollectedAsy: function() {
    var that = this;
    wx.getStorage({
      key: 'posts_collected',
      success: function(res) {
        console.log(res);
        var postId = that.data.postId;
        var postsCollected = res.data;
        var postCollected = postsCollected[postId];
        // 点击取反操作
        postCollected = !postCollected;
        // 更新该篇文章的状态值
        postsCollected[postId] = postCollected;
        // this.showToast(postsCollected, postCollected)
        that.showToast(postsCollected, postCollected)
      }
    })
  },
  // 显示显示消息提示框
  showToast: function(postsCollected, postCollected) {
    this.setData({
      collected: postCollected
    })
    // 重新对所有文章的状态对象存储起来
    wx.setStorageSync("posts_collected", postsCollected);
    // 收藏、取消收藏弹窗
    wx.showToast({
      title: postCollected ? "收藏成功" : "取消收藏",
      icon: "success",
      duration: 1000
    })
  },
  // 显示显示模态对话框
  showModal: function(postsCollected, postCollected) {
    var that = this;
    wx.showModal({
      title: "收藏",
      content: postCollected ? '是否收藏该文章？' : "是否取消该文章？",
      showCancel: true,
      cancelText: "取消",
      cancelColor: "#333",
      confirmText: "确定",
      confirmColor: "#405f80",
      success: function(res) {
        if (res.confirm) {
          that.setData({
            collected: postCollected
          })
          // 重新对所有文章的状态对象存储起来
          wx.setStorageSync("posts_collected", postsCollected);
        }
      }
    })
  },
  // 显示显示操作菜单 
  onShareTap: function(event) {
    var itemList = [
      "分享给好友",
      "分享到朋友圈",
      "分享给QQ好友",
      "分享到QQ空间",
      "分享到微博"
    ];
    wx.showActionSheet({
      itemList: itemList,
      itemColor: "#405f80",
      success: function(res) {
        // wx.showModal({
        //   title: '用户' + itemList[res.tapIndex],
        //   content: '用户是否取消?' + res.cancel + "现在无法实现分享功能，什么时候能实现呢？"
        // })
        console.log(res);
      }
    })
  },
  // 音乐播放功能
  onMusicTap: function(event) {
    var postData = this.data;
    var isPlayingMusic = postData.isPlayingMusic;
    if (isPlayingMusic) {
      wx.pauseBackgroundAudio();
      this.setData({
        isPlayingMusic: false
      })
    } else {
      wx.playBackgroundAudio({
        dataUrl: postData.music.url,
        title: postData.music.title,
        coverImgUrl: postData.music.coverImg
      });
      this.setData({
        isPlayingMusic: true
      })
    }
  }

})